package com.ny.zmb.glxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.entity.PmStoreExt;
import com.ny.zmb.glxt.mapper.PmStoreExtMapper;
import com.ny.zmb.glxt.mapper.PmStoreMapper;
import com.ny.zmb.glxt.service.PmStoreExtService;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.UploadDocumentReqCommandPlus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PmStoreExtServiceImpl extends ServiceImpl<PmStoreExtMapper, PmStoreExt> implements PmStoreExtService {
    @Resource
    private PmStoreMapper pmStoreMapper;
    @Resource
    private PmStoreExtMapper pmStoreExtMapper;

    QueryWrapper<PmStore> pmStoreQueryWrapper = new QueryWrapper<>();
    QueryWrapper<PmStoreExt> pmStoreExtQueryWrapper = new QueryWrapper<>();

    @Override
    public boolean infoUpdate(Integer userId, UploadDocumentReqCommandPlus obj, String ossPath) {
        pmStoreExtQueryWrapper = new QueryWrapper<>();
        pmStoreExtQueryWrapper.eq("user_id", userId);

        PmStoreExt pmStoreExt = pmStoreExtMapper.selectOne(pmStoreExtQueryWrapper);
        if (pmStoreExt == null) {

            QueryWrapper<PmStore> pmStoreQueryWrapper = new QueryWrapper<>();
            pmStoreQueryWrapper.eq("user_id", userId);
            PmStore pmStore = pmStoreMapper.selectOne(pmStoreQueryWrapper);
            if (pmStore == null) {
                pmStore = new PmStore(userId);
                pmStoreExtMapper.insert(pmStoreExt);
            }
            pmStoreExt = new PmStoreExt();
            pmStoreExt.setUserId(userId);
            pmStoreExt.setStoreId(pmStore.getId());
        }
        String type = obj.getImgType();
        switch (type) {
            case "LOGO":
                pmStoreExt.setLogo(ossPath);
                break;
            case "Banner":
                pmStoreExt.setBanner(ossPath);
                break;
            case "电子协议签名":
                pmStoreExt.setElectronicProtocolSignature(ossPath);
                break;
            case "对赌协议签名":
                pmStoreExt.setSignatureOfBettingAgreement(ossPath);
                break;
            case "担保协议签名":
                pmStoreExt.setSignatureOfGuaranteeAgreement(ossPath);
                break;
            case "营业执照":
                pmStoreExt.setBusinessPicUrl(ossPath);
                break;
            case "结算账户卡正面":
                pmStoreExt.setAccountCardPicFrontUrl(ossPath);
                break;
            case "法人证件正面":
                pmStoreExt.setLawyerIdPicFrontUrl(ossPath);
                break;
            case "法人证件反面":
                pmStoreExt.setLawyerIdPicBackUrl(ossPath);
                break;
            case "法人手持证件正面":
                pmStoreExt.setLawyerIdHandFrontUrl(ossPath);
                break;
            case "门头照图片":
                pmStoreExt.setDoorFrontPicUrl(ossPath);
                break;
            case "收银台照图片":
                pmStoreExt.setCashierPicUrl(ossPath);
                break;
            case "场景照图片":
                pmStoreExt.setScenePicUrl(ossPath);
                break;
            default:
                throw new RuntimeException("参数错误" + "没有找到 " + type + " 方法");
        }
        return saveOrUpdate(pmStoreExt);
    }

    @Override
    public PmStoreExt getInfoByOutMerchantNo(String outMerchantNo) {
        pmStoreExtQueryWrapper = new QueryWrapper<>();
        pmStoreExtQueryWrapper.eq("out_merchant_no", outMerchantNo);
        return pmStoreExtMapper.selectOne(pmStoreExtQueryWrapper);
    }

    @Override
    public PmStoreExt getInfoByUserId(Integer userId) {
        pmStoreExtQueryWrapper = new QueryWrapper<>();
        pmStoreExtQueryWrapper.eq("user_id", userId);
        return pmStoreExtMapper.selectOne(pmStoreExtQueryWrapper);
    }
}
